<template>
  <el-form :model="regionForm" status-icon :rules="rules" ref="regionForm"
           label-width="auto">
    <h2 class="region_title">系统注册</h2>
    <el-form-item prop="openId" label="账号">
      <el-input type="text" v-model="regionForm.openId"
                placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="email" label="个人邮箱">
      <el-input type="text" v-model="regionForm.email"
                placeholder="邮箱号"></el-input>
    </el-form-item>
    <el-form-item prop="name" label="用户名">
      <el-input type="text" v-model="regionForm.name"></el-input>
    </el-form-item>
    <el-form-item label="输入密码" prop="password">
      <el-input type="password" v-model="regionForm.password"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="再次输入" prop="check">
      <el-input type="password" v-model="regionForm.check"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
  </el-form>
</template>
<script>
  export default {
    name: "Regionform",
    data() {
      var valiEmail = (rule, value, callback) => {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
        if (value === '') {
          callback(new Error('邮箱号不为空'));
        }
        setTimeout(() => {
          if (mailReg.test(value)) {
            callback()
          } else {
            callback(new Error('请输入正确的邮箱格式'))
          }
        }, 100);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.regionForm.check !== '') {
            this.$refs.regionForm.validateField('check');
          }
          callback();
        }
      };
      var valiCheck = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.regionForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      var valiName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('用户名不能为空'));
        } else {
          callback();
        }
      };
      var valiOpenId = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('用户名不能为空'));
        } else {
          callback();
        }
      };
      return {
        regionForm: {
          email: '',
          password: '',
          check: '',
          name: '',
          openId: ''
        },
        rules: {
          email: [
            {validator: valiEmail, trigger: 'blur'}
          ],
          password: [
            {validator: validatePass, trigger: 'blur'}
          ],
          check: [
            {validator: valiCheck, trigger: 'blur'}
          ],
          name: [
            {validator: valiName, trigger: 'blur'}
          ],
          openId: [
            {validator: valiOpenId, trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm() {
        this.$refs['regionForm'].validate((valid) => {
          if (valid) {
            this.$axios
              .post('/api/agent/register', {
                agentOpenid: this.regionForm.openId,
                email: this.regionForm.email,
                agentName: this.regionForm.name,
                passWord: this.regionForm.check

              })
              .then(success => {
                this.$message({
                  type: 'info',
                  message: "注册成功！"
                });
              }).catch(fail => {
              this.$message.error("注册失败")
            })
          } else {
            this.$message.error("编辑信息有误")
            return false;
          }
        });
      },
      resetForm() {
        this.$refs['regionForm'].resetFields();
      }
    }
  }
</script>

<style scoped>
  .region_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: darkslateblue;
  }

</style>
